<?php

class AmpMenuHelper extends Helper
{

  /**
   *
   * @param $menu Menu
   * @return string
   */
  public function render($menu)
  {
    return $this->_render($menu);
  }

  /**
   *
   * @param $menu Menu
   * @return string
   */
  protected function _render($menu, $depth = 0)
  {
    if (empty($menu)) return '';
    
    $ul = new HTMLElement('ul', array(
      'class'=>'sf-menu menu level_' . $depth
    ));
    
    $i = 0;
    while ($i < $menu->size()) {
      $ul->appendInner($this->_renderItem($menu->getItemAt($i ++), $depth));
    }
    
    return $ul;
  }

  /**
   *
   * @param $item MenuItem
   * @return string
   */
  protected function _renderItem($item, $depth = 0)
  {
    if (empty($item)) return '';
    
    $submenu = $this->_render($item->submenu, $depth + 1);
    
    $active = $item->match($this->request->get('route'), ' active');
    
    $label = is_null($item->url) ? new HTMLElement('span', array(
      'class'=>'label'
    ), null, $item->label) : new HTMLElement('a', array(
      'class'=>'label', 
      'href'=>$item->url, 
      'title'=>$item->label
    ), null, $item->label);
    
    $li = new HTMLElement('li', array(
      'class'=>'menu_item level_' . $depth . $active
    ), null, $label, $submenu);
    
    return $li;
  }
}

?>